package com.zmn.oms.third.koubei.service.impl;

import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.third.koubei.KoubeiConsts;
import com.zmn.oms.third.koubei.service.KoubeiService;
import com.zmn.oms.zmn.business.interfaces.open.OrderChangeNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 天猫勤鸽渠道对接订单状态处理实现
 *
 * @author xiewenbing
 * @date  2021/07/14 11:30
 */
@Slf4j
@Service("koubeiNotifyService")
public class KoubeiNotifyServiceImpl implements OrderChangeNotifyService {

    @Autowired
    private KoubeiService koubeiService;

    @Override
    public void notifyChange(OrderWorkLogChange orderWorkLogChange) {

        // 判断功能是否开启
        if (!KoubeiConsts.POWER_ON) {
            log.info("【{}】<订单状态同步> 该功能没有开启。", KoubeiConsts.CHANNEL_NAME);
            return;
        }

        // 参数判断
        if (Objects.isNull(orderWorkLogChange)) {
            log.info("【{}】<订单状态同步> 参数为null", KoubeiConsts.CHANNEL_NAME);
            return;
        }

        // 录入方式和渠道订单id判断
        if (!Objects.equals(orderWorkLogChange.getInputType(),OrderConsts.ORDER_INPUT_TYPE_API) ||
                StringUtil.isBlank(orderWorkLogChange.getOuterId())) {
            log.info("【{}】的录入方式不为API接入，不需要状态同步", KoubeiConsts.CHANNEL_NAME);
            return;
        }

        log.info("【{}】<订单状态同步> ,状态信息:[{}]。", KoubeiConsts.CHANNEL_NAME,orderWorkLogChange.toString());

        int logType = orderWorkLogChange.getLogType().intValue();

        try {
            switch (logType) {

                // 领单
                case OrderLogConsts.ORDER_LOG_TYPE_TAKE:
                    koubeiService.syncOrderTake(orderWorkLogChange);

                    break;

                /** 不同步  2021/08/26
                // 修改、改约
                case OrderLogConsts.ORDER_LOG_TYPE_CONFIRM:
                case OrderLogConsts.ORDER_LOG_TYPE_UPDATE:
                case OrderLogConsts.ORDER_LOG_TYPE_MODIFY_DUTY_TIME:
                case OrderLogConsts.ORDER_LOG_TYPE_MASTER_MODIFY_DUTY_TIME:
                    koubeiService.syncOrderDutytime(orderWorkLogChange);
                    break;

                 // 取消、失败、弃单
                case OrderLogConsts.ORDER_LOG_TYPE_CANCEL:
                case OrderConsts.ORDER_OP_TYPE_FAIL:
                case OrderConsts.ORDER_OP_TYPE_DISCARD:
                    koubeiService.syncOrderCancel(orderWorkLogChange);
                    break;
                 */

                // 上门
                case OrderLogConsts.ORDER_LOG_TYPE_VISIT:
                    koubeiService.syncOrderVisit(orderWorkLogChange);
                    break;

                // 完成
                case OrderLogConsts.ORDER_LOG_TYPE_COMPLETE:
                    koubeiService.syncOrderComplete(orderWorkLogChange);
                    break;

                default:
                    log.info("【{}】<订单状态-【{}】不同步> ", KoubeiConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType));
                    return;
            }
        } catch (OmsBaseException exception) {
            log.info("【{}】<订单状态-【{}】同步> 失败 【{}】 ", KoubeiConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType),
                    exception.getMessage());
        }

        log.info("【{}】<订单状态-【{}】同步> 完成 ", KoubeiConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType));
    }
}
